import os
import re
import sys
import chardet

__author__ = 'Coolcow'

reload(sys)
sys.setdefaultencoding('utf8')

def file_dir(base):
    list_files = []
    for path, dirs, files in os.walk(base):
        list_files.extend([os.path.join(path, f) for f in files if f.endswith('.java')])
    list_files.sort()
    # print list_files
    for filename in list_files:
        print filename
        modify(filename)

def modify(f):
    # place = re.compile('^\xEF\xBB\xBF', re.M)
    # print place

    with open(f, 'r') as obj:
        text = obj.read()

        # text = place.sub("", text)
        # text = unicode(text, errors='replace')
        # text = text.encode('utf-8', 'ignore')

        enc = chardet.detect(text)
        print enc['encoding']
        text = text.decode(enc['encoding']).encode('utf-8')

    with open(f, 'wb') as obj:
        obj.write(text)

# def modify_detect(f):
#     with open(f, 'r') as obj:
#         text = obj.read()
#         enc = chardet.detect(text)
#         text.decode(enc['encoding']).encode('utf-8')
#         print enc['encoding']

if __name__ == '__main__':
    base_dir = os.path.dirname(__file__)
    if len(sys.argv) > 1:
        base_dir = sys.argv[1]
    print base_dir
    file_dir(base_dir)
